#ifndef __WEXT_CPP__
#define __WEXT_CPP__

#include "whead.h"
#include "wextfunc.h"
#include "wext.h"

using namespace WExtFunc;

void ExtFunction(struct WServer::MsgHandle::wmsgdata* msg)
{
	baExecption.reset(msg->clientindex, msg->type, msg->len, msg->memindex);
	if (msg->clientindex < 0 || msg->clientindex > GData::maxconn) return;
	try {
		byteArrays ba(msg->type, msg->getparam(), msg->paramlen());
		switch (msg->type) {
			case WMSG_TIMEOUT: {
				MsgTimeout msgtimeout;msgtimeout.decodeMsgClass(ba);msg_timeout(msgtimeout.timeoutid, &msgtimeout);
				break;
			}
			case WMSG_LOGINATTEMPT: {
				MsgLoginAttempt msgloginattempt;msgloginattempt.decodeMsgClass(ba);
				CreateMessage2WMysql(msg->clientindex, msgloginattempt);
				break;
			}
			case WMSG_LOGINFAIL: {
				MsgLoginFail msgloginfail;msgloginfail.decodeMsgClass(ba);msg_loginfail(msg->clientindex, &msgloginfail);
				break;
			}
			case WMSG_LOGINSUCCESS: {
				MsgLoginSuccess msgloginsuccess;msgloginsuccess.decodeMsgClass(ba);msg_loginsuccess(msg->clientindex, &msgloginsuccess);
				break;
			}
		}
	}
	catch (ByteArrayException& e) {
		e.printdata();
		return;
	}
}

void InitExt()
{
	pthread_mutex_init(&pfreeobject, 0);
	curobject = 1;
	objectlist = new objectdata[maxobject];
	memset(objectlist, 0, sizeof(objectdata)*maxobject);

	g_pSafePlayer = new CPlayer(0);
	clientlist[0].objectid = 0;
	g_pSafePlayer->objectid = 0;
	objectlist[0].objectpoint = g_pSafePlayer;
}

#endif //__WEXT_CPP__
